package com.graduation.vaccine.controller.web;

import com.graduation.vaccine.service.ProductionService;
import com.graduation.vaccine.service.UserService;
import com.graduation.vaccine.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/5/1 17:54
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/web/production")
@Api(tags = "web——疫苗模块")
public class WebProductionController {

    @Autowired
    private ProductionService productionService;
    @Autowired
    private UserService userService;

    @ApiOperation("5/01——01:疫苗列表（组织层也采取这个接口）")
    @ResponseBody
    @PostMapping("/list")
    public RespBean getProductionList(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator"))){
//                return vaccineCatalogService.sysGetList();
                //返回全部列表
                return productionService.getProductionList();
            }else if(userVo.getAction().equals("organization_administrator") || userVo.getAction().equals("organization_member")){
                return productionService.getOrgProductionList(userVo.getDepartmentCode());
            }

            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("5/01——01：添加疫苗（仅限系统管理员）,种类下拉框见种类模块")
    @ResponseBody
    @PostMapping("/add")
    public RespBean addOne(@RequestBody AddProductionVo addProductionVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=addProductionVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator"))){
                //返回全部列表
                return productionService.addOne(addProductionVo);
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("5/01-01：修改疫苗信息")
    @ResponseBody
    @PostMapping("/update")
    public RespBean updateOne(@RequestBody UpdateProductionVo updateProductionVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=updateProductionVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator"))){
                //返回全部列表
                return productionService.updateOne(updateProductionVo);
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("5/01-01:疫苗入库（仅组织管理员可操作）")
    @ResponseBody
    @PostMapping("/add/stock")
    public RespBean addStock(@RequestBody AddStockVo addStockVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=addStockVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("organization_administrator"))){
                //获取登录用户的部门
                String departmentCode=userVo.getDepartmentCode();
                return productionService.addStock(addStockVo.getProductionId(),addStockVo.getNumber(),departmentCode);
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

}
